<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>添加网站·思通投研·让数据驱动投资价值!</title>
    <link href="${pageContext.request.contextPath }/img/webLogoIcon.png" rel="shortcut icon">
    <link href="${pageContext.request.contextPath }/css/bootstrap.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath }/css/font-awesome.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath }/css/custom.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath }/css/animate.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath }/css/style.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath }/css/datatables.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath }/css/datepicker3.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath }/css/toastr.min.css" rel="stylesheet"/>
    <link href="${pageContext.request.contextPath }/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">

    <style>
        .left {
            float: left;
            width: 33%;
        }

        .form-horizontal {
            width: 95%;
        }

        .form-control {
            display: inline;
        }

        .col-sm-2 {
            height: 50px;
        }
    </style>
</head>

<body>

<div id="wrapper">

    <nav class="navbar-default navbar-static-side" role="navigation">
        <%request.setAttribute("LEFT", "websitelist");%>
        <%@include file="../left.jsp" %>
    </nav>

    <div id="page-wrapper" class="gray-bg">
        <div class="row border-bottom">
            <jsp:include page="../head.jsp" flush="true"/>
        </div>
        <div class="row wrapper border-bottom white-bg page-heading">
            <div class="col-lg-10">
                <h2>添加网站</h2>
            </div>
        </div>
        <div class="wrapper wrapper-content">
            <div class="ibox-content">
                <button class="btn btn-white pull-right"
                        onclick="window.location.href = '${pageContext.request.contextPath}/admin/website_admin'"
                        type="button">返回
                </button>
                <div class="table-responsive" style="margin-top: 5%;">

                    <div class="panel-body">
                        <fieldset class="form-horizontal">
                            <div class="form-group"><label class="col-sm-2 control-label">网站类型:<em style="color: red">*</em></label>
                                <div class="col-sm-10">
                                    <select class="form-control" style="display: inline;width: 35%;"
                                            id="website_type">
                                        <option value="0">请选择网站类型</option>
                                        <option value="1">微信</option>
                                        <option value="2">微博</option>
                                        <option value="3">政务</option>
                                        <option value="4">论坛</option>
                                        <option value="5">新闻</option>
                                        <option value="6">报刊</option>
                                        <option value="7">客户端</option>
                                        <option value="8">网站</option>
                                        <option value="9">外媒</option>
                                        <option value="10">视频</option>
                                        <option value="11">博客</option>
                                        <option value="12">自媒体</option>
                                        <option value="13">招投标</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group"><label class="col-sm-2 control-label">网站名称:<em style="color: red">*</em></label>
                                <div class="col-sm-10"><input type="text" class="form-control" id="website_name"
                                                              style="width: 88%;" placeholder="请输入网站名称"></div>
                            </div>
                            <div class="form-group"><label class="col-sm-2 control-label">网站地址:<em style="color: red">*</em></label>
                                <div class="col-sm-10"><input onblur="getdomain(this)" type="text" class="form-control"
                                                              id="website_url"
                                                              style="width: 88%;" placeholder="请输入网站地址"></div>
                            </div>
                            <div class="form-group"><label class="col-sm-2 control-label">一级域名:<em style="color: red"></em></label>
                                <div class="col-sm-10"><input readonly type="text" class="form-control"
                                                              id="one_domain"
                                                              style="width: 88%;" placeholder="一级域名"></div>
                            </div>
                            <div class="form-group"><label class="col-sm-2 control-label">二级域名:<em style="color: red"></em></label>
                                <div class="col-sm-10"><input readonly type="text" class="form-control"
                                                              id="two_domain"
                                                              style="width: 88%;" placeholder="二级域名"></div>
                            </div>
                            <div class="form-group"><label class="col-sm-2 control-label">网站logo:<em style="color: red">*</em></label>
                                <div class="col-sm-10"><input type="text" class="form-control" id="website_ico"
                                                              style="width: 88%;" placeholder="请输入网站logo"></div>
                            </div>
                            <div class="form-group"><label class="col-sm-2 control-label">网站服务器所在省:<em style="color: red"></em></label>
                                <div class="col-sm-10"><input type="text" class="form-control" id="website_province"
                                                              style="width: 88%;" placeholder="网站服务器所在省"></div>
                            </div>
                            <div class="form-group"><label class="col-sm-2 control-label">网站服务器所在市:<em style="color: red"></em></label>
                                <div class="col-sm-10"><input type="text" class="form-control" id="website_city"
                                                              style="width: 88%;" placeholder="网站服务器所在市"></div>
                            </div>
                            <div class="form-group"><label class="col-sm-2 control-label">自定义排名:<em style="color: red"></em></label>
                                <div class="col-sm-10"><input maxlength="2" type="number" onkeyup="onlyNumber(this)"
                                                              class="form-control" id="site_rank" onchange="onlyNumber(this)"
                                                              style="width: 88%;" placeholder="10最大,0最小"></div>
                            </div>

                            <div class="form-group"><label class="col-sm-2 control-label">是否是主流站点:<em style="color: red"></em></label>
                                <div class="col-sm-10">
                                    <select class="form-control" style="display: inline;width: 15%;"
                                            id="mainstream_flag">
                                        <option value="2">否</option>
                                        <option value="1">是</option>
                                    </select>
                                </div>
                            </div>

                            <div class="form-group"><label class="col-sm-2 control-label">网站备注:<em style="color: red"></em></label>
                                <div class="col-sm-10">
                                    <textarea class="form-control " id="website_remark" style=" height:100px;width: 88%;" placeholder="请输入网站备注"></textarea>
                                </div>
                            </div>
                        </fieldset>
                        <button class="btn btn-primary" style="margin-left: 45%;margin-top: 2%;" onclick="addWebSite()"
                                type="button">确定添加网站
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <jsp:include page="../footer.jsp" flush="true"/>
    </div>
</div>


<!-- Mainly scripts -->
<script src="${pageContext.request.contextPath }/js/jquery-2.1.1.js"></script>
<script src="${pageContext.request.contextPath }/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath }/js/jquery.metisMenu.js"></script>
<script src="${pageContext.request.contextPath }/js/jquery.slimscroll.min.js"></script>
<script src="${pageContext.request.contextPath }/js/jquery.jeditable.js"></script>
<script src="${pageContext.request.contextPath }/js/datatables.min.js"></script>

<!-- Peity -->
<script src="${pageContext.request.contextPath }/js/jquery.peity.min.js"></script>

<!-- Custom and plugin javascript -->
<script src="${pageContext.request.contextPath }/js/inspinia.js"></script>
<script src="${pageContext.request.contextPath }/js/pace.min.js"></script>

<!-- Peity -->
<script src="${pageContext.request.contextPath }/js/peity-demo.js"></script>
<!-- toastr -->
<script src="${pageContext.request.contextPath }/js/plugins/toastr/toastr.min.js"></script>
<!-- Sweet alert -->
<script src="${pageContext.request.contextPath }/js/plugins/sweetalert/sweetalert.min.js"></script>

<!-- Page-Level Scripts -->
<script>
    // 一级域名提取
    var RE_TOP_1 = /^(\w*\.?){1}\.(com\.cn|net\.cn|gov\.cn|org\.nz|org\.cn|com|net|org|gov|cc|biz|info|cn|co|me)$/;

    // 二级域名提取
    var RE_TOP_2 = /^(\w*\.?){2}\.(com\.cn|net\.cn|gov\.cn|org\.nz|org\.cn|com|net|org|gov|cc|biz|info|cn|co|me)$/;


    var reg = /^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/;


    function getdomain(obj) {
        if(!obj.value){
            return false;
        }

        var url = obj.value;
        if (!reg.test(url)) {
            swal("请检查", "该url不是一个正确的网址", "error");
            return false;
        }
        /*var exampleUrl = new URL(url);
        var host = exampleUrl.host;
        var one_domain = host.match(RE_TOP_1);
        var two_domain = host.match(RE_TOP_2);
        console.log(one_domain)
        console.log(two_domain)
        if(one_domain){
            $("#one_domain").val(one_domain[0]);
        }

        if(two_domain){
            $("#two_domain").val(two_domain[0]);
        }*/
        $.ajax({
            url: "${pageContext.request.contextPath}/datasourcesite/getDomain",
            type: "post",
            data: {
              url : url
            },
            success: function (res) {
                $("#one_domain").val(res.one_domain);
                $("#two_domain").val(res.two_domain);
            }
        });

    }

    function onlyNumber(obj) {
        //先把非数字的都替换掉，除了数字和.和-号
        obj.value = obj.value.replace(/[^\d]/g, "");
        //前两位不能是0加数字
        obj.value = obj.value.replace(/^0\d[0-9]*/g, '');
        //必须保证第一个为数字而不是.
        obj.value = obj.value.replace(/^\./g, '');

        obj.value = obj.value >= 10 ? 10 : obj.value <= 0 ? 0 : obj.value;
    }

    function addWebSite() {
        if(!$("#website_type").val() || $("#website_type").val() == 0){
            swal("请检查", "网站类型不能为空", "error");
            return false;
        }
        if(!$("#website_name").val()){
            swal("请检查", "网站名称不能为空", "error");
            return false;
        }
        if(!$("#website_url").val()){
            swal("请检查", "网站地址不能为空", "error");
            return false;
        }
        if(!$("#website_ico").val()){
            swal("请检查", "网站logo不能为空", "error");
            return false;
        }


        swal({
                title: "确定添加该网站?",
//          text: "Your will not be able to recover this imaginary file!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: false,
                closeOnCancel: true
            },
            function (isConfirm) {
                if (isConfirm) {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/datasourcesite/saveWeb",
                        type: "post",
                        data: {
                            websiteName: $("#website_name").val(),
                            websiteUrl: $("#website_url").val(),
                            websiteRemark: $("#website_remark").val(),

                            newWebsiteType: $("#website_type").val(),
                            websiteIco: $("#website_ico").val(),
                            mainstreamFlag: $("#mainstream_flag").val(),
                            websiteCity:$("#website_city").val(),
                            websiteProvince:$("#website_province").val(),
                            siteRank:$("#site_rank").val(),
                            manuallyReview:1,
                            intelligentMatch:1,
                            rounds:0,
                            messageStatus: 2
                        },
                        success: function (res) {
                            if (res.status == 200) {
                                swal("添加成功", "该网站已添加成功", "success");
                                window.location.href = "${pageContext.request.contextPath}/admin/website_admin"
                            } else {
                                swal("添加失败", "该网站添加失败", "error");
                            }
                        },
                        error: function (e) {
                            swal("添加失败", "该网站添加失败", "error");
                            window.clearInterval(timer);
                        }
                    });
                } else {

                }
            });


    }


</script>

</body>

</html>
